<?php
session_start();
include "header.php";
include "input_cleaners.php";

if( isset($_POST['Submit']))
{
  $strName = $_REQUEST['Name'];
  #$strLastName  = $_REQUEST['Last_Name'];
  $strDataUse   = $_REQUEST['DataUse'];
  $strOrg       = $_REQUEST['Organization'];
  $strEmail     = $_REQUEST['Email'];
  if( strlen( $strEmail ) )
  {
    if( validEmail( $strEmail ) == false )
    {
      echo( "Invalid email: $strEmail provided. Please provide a valid email address." );
      $strEmail = '';
      return;
    }
  }
  $strPurpose   = $_REQUEST['Purpose'];
  $strComment   = $_REQUEST['Comments'];
  $iEaseOfUse   = -1;
  if( strcmp( $_REQUEST['EaseOfUseCombo'], 'NA' ) != 0 )
  {
    $iEaseOfUse   = $_REQUEST['EaseOfUseCombo']; 
  }
  
  $iDataUseful  = -1;
  if( strcmp( $_REQUEST['DataUsefullnessCombo'], 'NA' ) != 0 )
  {
    $iDataUseful   = $_REQUEST['DataUsefullnessCombo']; 
  }
  
  $iRecommend   = -1;
  if( strcmp( $_REQUEST['RecommendCombo'], 'NA' ) != 0 )
  {
    $iRecommend   = $_REQUEST['RecommendCombo']; 
  }
  $bwriteHeader = true;
  if( file_exists( './survey/survey.csv' ) )
  {
    $bwriteHeader = false;
  }
  $File = fopen('./survey/survey.csv', 'a+');
  if( $File )
  {
    if( $bwriteHeader == true )
    {
      $OutBuf = "Date,Full Name,Email,Organization,Occupation,Purpose,EaseOfUse,DataUseful,RecommendToOthers,Comments\n";
      fwrite( $File, $OutBuf );
    }
    $strEntryDate = `date +%x-%T`;
    $strEntryDate = trim( $strEntryDate );
  	$OutBuf = "$strEntryDate,\"$strName\",$strEmail,$strOrg,$strDataUse,$strPurpose,$iEaseOfUse,$iDataUseful,$iRecommend,\"$strComment\"\n";
  	fwrite( $File, $OutBuf );
  	fclose( $File );
  }
  
  #echo( "strFirstName: $strFirstName strLastName: $strLastName strDataUse: $strDataUse strOrg: $strOrg strEmail: $strEmail strPurpose: $strPurpose"); 
}

?>

<table>
  <tbody>
    <tr>
      <td>
      <h2>
<?php
        echo( "$strName, thank you for your feedback.\n" );
?>        
      </h2>
     </td>
   </tr>
 </tbody>
</table>
<?php
include "footer.html";
?>
